package org.ilovecode.display {
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import org.ilovecode.events.MenuEvent;
	import org.ilovecode.mvc.View;
	import org.ilovecode.screen.ScreenName;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class MainMenu extends View {
		
		//{ region Stage elements
		
		public var aboutButton:SimpleButton;
		public var apartmentsButton:SimpleButton;
		public var garageButton:SimpleButton;
		public var paymentConditionsButton:SimpleButton;
		public var faqButton:SimpleButton;
		public var contactButton:SimpleButton;
		public var apartmentsMenu:ApartmentsMenu;
		public var paymentMenu:PaymentMenu;
		
		//} endregion
		
		private var timer:Timer;
		private var holdTimer:Timer;
		private var isMouseOut:Boolean = false;
		private var _mode:String = "";
		
		public function MainMenu() {
			super("mainMenu");
			//this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
			
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			apartmentsButton.addEventListener(MouseEvent.MOUSE_OVER, apartmentButtonMouseOver);
			apartmentsButton.addEventListener(MouseEvent.MOUSE_OUT, apartmentButtonMouseOut);
			apartmentsButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			apartmentsMenu.addEventListener(MenuEvent.CLICK, apartmentsMenuClick);
			aboutButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			garageButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			contactButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			faqButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			paymentConditionsButton.addEventListener(MouseEvent.MOUSE_OVER, paymentConditionsButtonMouseOver);
			paymentConditionsButton.addEventListener(MouseEvent.MOUSE_OUT, paymentConditionsButtonMouseOut);
			paymentMenu.addEventListener(MenuEvent.CLICK, paymentMenuClick);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			apartmentsButton.removeEventListener(MouseEvent.MOUSE_OVER, apartmentButtonMouseOver);
			apartmentsButton.removeEventListener(MouseEvent.MOUSE_OUT, apartmentButtonMouseOut);
			apartmentsButton.removeEventListener(MouseEvent.CLICK, menuButtonClick);
			apartmentsMenu.removeEventListener(MenuEvent.CLICK, apartmentsMenuClick);
			aboutButton.removeEventListener(MouseEvent.CLICK, menuButtonClick);
			garageButton.removeEventListener(MouseEvent.CLICK, menuButtonClick);
			contactButton.removeEventListener(MouseEvent.CLICK, menuButtonClick);
			faqButton.removeEventListener(MouseEvent.CLICK, menuButtonClick);
			paymentConditionsButton.removeEventListener(MouseEvent.MOUSE_OVER, paymentConditionsButtonMouseOver);
			paymentConditionsButton.removeEventListener(MouseEvent.MOUSE_OUT, paymentConditionsButtonMouseOut);
			paymentMenu.removeEventListener(MenuEvent.CLICK, paymentMenuClick);
		}
		
		/*
		private function addedToStage(event:Event):void {
			init();
		}
		
		private function init():void {
			initListeners();
		}
		
		private function initListeners():void {
			apartmentsButton.addEventListener(MouseEvent.MOUSE_OVER, apartmentButtonMouseOver);
			apartmentsButton.addEventListener(MouseEvent.MOUSE_OUT, apartmentButtonMouseOut);
			apartmentsButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			apartmentsMenu.addEventListener(MenuEvent.CLICK, apartmentsMenuClick);
			aboutButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			garageButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			contactButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			faqButton.addEventListener(MouseEvent.CLICK, menuButtonClick);
			paymentConditionsButton.addEventListener(MouseEvent.MOUSE_OVER, paymentConditionsButtonMouseOver);
			paymentConditionsButton.addEventListener(MouseEvent.MOUSE_OUT, paymentConditionsButtonMouseOut);
			paymentMenu.addEventListener(MenuEvent.CLICK, paymentMenuClick);
		}*/
		
		private function menuButtonClick(event:MouseEvent):void {
			var name:String = String(event.currentTarget.name).replace("Button", "");
			//this.dispatchEvent(new MenuEvent(MenuEvent.CLICK, name));
			var screenName:String;
			switch(name)
			{
				case "apartments":
					screenName = ScreenName.RENDER;
					break;
				case "about":
					screenName = ScreenName.ABOUT_PROJECT;
					break;
				case "garage":
					screenName = ScreenName.GARAGE;
					break;
				case "contact":
					screenName = ScreenName.CONTACT;
					break;
				case "faq":
					screenName = ScreenName.FAQ;
					break;
				default:
					break;
			}
			controller.showScreen(screenName);
		}
		
		private function apartmentsMenuClick(event:MenuEvent):void {
			//this.dispatchEvent(event);
			var screenName:String;
			switch(event.item)
			{
				case "searchAll":
					screenName = ScreenName.SEARCH;
					break;
				case "description":
					screenName = ScreenName.ABOUT;
					break;
				case "gallery":
					screenName = ScreenName.GALLERY;
					break;
				default:
					break;
			}
			controller.showScreen(screenName);
		}
		
		private function paymentMenuClick(event:MenuEvent):void {
			//this.dispatchEvent(event);
			var screenName:String;
			switch(event.item)
			{
				case "discount":
					screenName = ScreenName.DISCOUNT;
					break;
				case "conditions":
					screenName = ScreenName.CONDITIONS;
					break;
				default:
					break;
			}
			controller.showScreen(screenName);
		}
		
		//{ region Payment menu
		
		private function paymentConditionsButtonMouseOver(event:MouseEvent):void {
			paymentMenu.show();
		}
		
		private function paymentConditionsButtonMouseOut(event:MouseEvent):void {
			timer = new Timer(500, 1);
			timer.addEventListener(TimerEvent.TIMER, paymentTimerTick);
			timer.start();
		}
		
		private function paymentTimerTick(event:TimerEvent):void {
			if (!paymentMenu.isActive) {
				paymentMenu.hide();
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, paymentTimerTick);
			}
		}
		
		//} endregion
		
		//{ region Show apartments menu
		
		private function apartmentButtonMouseOver(event:MouseEvent):void {
			apartmentsMenu.show();
		}
		
		private function apartmentButtonMouseOut(event:MouseEvent):void {
			timer = new Timer(500, 1);
			timer.addEventListener(TimerEvent.TIMER, apartmentTimerTick);
			timer.start();
		}
		
		private function apartmentTimerTick(event:TimerEvent):void {
			if (!apartmentsMenu.isActive) {
				apartmentsMenu.hide();
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, apartmentTimerTick);
			}
		}
		
		//} endregion
		
	}

}